package com.ewzj.bigdata.mvp.model;

import android.app.Application;

import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.app.utils.Apputils;
import com.ewzj.bigdata.mvp.model.api.service.CommonService;
import com.ewzj.bigdata.mvp.model.entity.reqbeans.ReqAddCommentBean;
import com.ewzj.bigdata.mvp.model.entity.reqbeans.ReqIdBean;
import com.ewzj.bigdata.mvp.model.entity.reqbeans.ReqPageBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseData;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseListData;
import com.ewzj.bigdata.mvp.model.entity.resbean.CommentBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.ResCommentBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.ResIdBean;
import com.google.gson.Gson;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import com.jess.arms.di.scope.ActivityScope;

import javax.inject.Inject;

import com.ewzj.bigdata.mvp.contract.CommentListContract;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.functions.Function;


@ActivityScope
public class CommentListModel extends BaseModel implements CommentListContract.Model {
    private Gson mGson;
    private Application mApplication;
    private CommonService service;
    @Inject
    public CommentListModel(IRepositoryManager repositoryManager, Gson gson, Application application) {
        super(repositoryManager);
        this.mGson = gson;
        this.mApplication = application;
        this.service=repositoryManager.obtainRetrofitService(CommonService.class);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
        this.service=null;
    }

    @Override
    public Observable<List<CommentBean>> getCommentList(String id, int page_index) {
        ReqPageBean reqPageBean=new ReqPageBean(page_index, AppConstants.PAGE_NUM,id);
        return service.commentList(Apputils.json2Body(mGson.toJson(reqPageBean)))
                .map(new Function<BaseData<BaseListData<CommentBean>>, List<CommentBean>>() {
                    @Override
                    public List<CommentBean> apply(BaseData<BaseListData<CommentBean>> baseListDataBaseData) throws Exception {
                        return baseListDataBaseData.getData().getList();
                    }
                });
    }

    @Override
    public Observable<BaseData<ResCommentBean>> sendComment(String comment, String commentid, String userid) {
        ReqAddCommentBean reqAddCommentBean=new ReqAddCommentBean();
        reqAddCommentBean.setContent(comment);
        reqAddCommentBean.setId(commentid);
        return service.commentAdd(Apputils.json2Body(mGson.toJson(reqAddCommentBean)));
    }

    @Override
    public Observable<BaseData<ResCommentBean>> replyComment(String comment, String commentid, String userid) {
        ReqAddCommentBean reqAddCommentBean=new ReqAddCommentBean();
        reqAddCommentBean.setContent(comment);
        reqAddCommentBean.setId(commentid);
        reqAddCommentBean.setTo_user_id(userid);
        return service.commentReply(Apputils.json2Body(mGson.toJson(reqAddCommentBean)));
    }

    @Override
    public Observable<BaseData<ResIdBean>> pariseComment(String id) {
        ReqIdBean reqIdBean=new ReqIdBean(id);
        reqIdBean.setType("1");
        return service.commentPraise(Apputils.json2Body(mGson.toJson(reqIdBean)));
    }
}